﻿using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using WxLib.Core.Util;
using WxLib.Core.Serializers;
using WxLib.Core.RequestHandlers;
using WxLib.Common;
using WxLib.Business;
using WxLib.QRCode;
using wxLib.QRCode.Models;
using System.Threading;
using System.Text;

namespace wxLib.QRCode
{
    /// <summary>
    /// 公众号获取
    /// </summary>
    public class WxGzhQRCode
    {
        private string appid = "wxc9eb5602a0479c14";
        private string secret = "bd3641b9162d863dc70e86657a29653d";

        /// <summary>
        /// 获取或设置用于执行请求的RequestHandler
        /// </summary>
        public IPostRequestHandler RequestHandler;

        public IRequestHandler ReHandler;

        /// <summary>
        /// 构造函数
        /// </summary>
        public WxGzhQRCode()
        {
            RequestHandler = new RequestHandler(new JsonSerializer());
            ReHandler = new RequestHandler(new JsonSerializer());
        }

        ///// <summary>
        ///// 用户临时流水号生成二维码
        ///// </summary>
        ///// <param name="phone"></param>
        //public async Task<RQCodeUrlResponse> GetWxaCodeUnlimitsAsync(string serialid)
        //{
        //    WxAccessTokenResponse response = WxCommonApi.Instance().GetAccessToken(appid, secret);
        //    var relUri = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + response.access_token;

        //    string jsonStr = "{\"expire_seconds\": 604800, \"action_name\": \"QR_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"" + serialid + "\"}}}";
        //    return await RequestHandler.PostRequestAsync<RQCodeUrlResponse>(new Uri(relUri), jsonStr);
        //}

        /// <summary>
        /// 新增素材
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <param name="base64">图片base64</param>
        /// <returns></returns>
        public RQCodeUrlResponse addnews(string fileName, byte[] base64)
        {
            WxAccessTokenResponse response = WxCommonApi.Instance().GetAccessTokenQeg(appid, secret);
            var relUri = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + response.access_token + "&type=image";
            string resultUpload = HttpHelp.HttpUploadPhoto(relUri, fileName, base64);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<RQCodeUrlResponse>(resultUpload);//获取上传后返回的结果状态
        }

        /// <summary>
        /// 生成永久分享码
        /// </summary>
        /// <param name="str">码值</param>
        /// <returns></returns>
        public async Task<RQCodeUrlResponse> GetWxaCodeUnlimitsForeverAsync(string str)
        {
            WxAccessTokenResponse response = WxCommonApi.Instance().GetAccessTokenQeg(appid, secret);
            var relUri = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + response.access_token;

            string jsonStr = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"" + str + "\"}}}";
            return await RequestHandler.PostRequestAsync<RQCodeUrlResponse>(new Uri(relUri), jsonStr);
        }




        /// <summary>
        /// 公众号客服发送消息
        /// </summary>
        /// <param name="openID">用户openid</param>
        /// <param name="city">城市</param>
        /// <param name="word">字号</param>
        /// <param name="industry">行业</param>
        /// <param name="companyType">公司类型</param>
        /// <returns></returns>
        public async Task<RQCodeResponse> SendMessageAsync(string openID, string city = "", string word = "", string industry = "", string companyType = "")
        {

            WxAccessTokenResponse response = WxCommonApi.Instance().GetAccessTokenQeg(appid, secret);
            var relUri = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + response.access_token;
            string pagepath = "/pages/index/index";
            if (!string.IsNullOrEmpty(city))
            {
                pagepath = "/pages/detail/index?address=" + city + "&zihao=" + word + "&hangye=" + industry + "&leixing=" + companyType;
            }
            string jsonStr = "{\"touser\":\"" + openID + "\",\"msgtype\":\"miniprogrampage\",\"miniprogrampage\":{\"title\":\"您的核名结果报告\",\"appid\":\"wxf0e89e07f52c745f\",\"pagepath\":\"" + pagepath + "\",\"thumb_media_id\":\"_o7IDuHHFKMFT8RvzNkVAIsY5ZMcjqK88MLbVu3UH8s\"}}";
            return await RequestHandler.PostRequestAsync<RQCodeResponse>(new Uri(relUri), jsonStr);
        }

        /// <summary>
        /// 发送模板信息
        /// </summary>
        /// <param name="openID">推送人openid</param>
        /// <param name="first">[徐锐]您好，有新的客户分配给你</param>
        /// <param name="keyword1">客户来源 PC端</param>
        /// <param name="keyword2">客户姓名 徐锐</param>
        /// <param name="keyword3">客户电话 17751654676</param>
        /// <param name="keyword4">客户备注</param>
        /// <param name="keyword5">分配时间</param>
        /// <param name="remark">请及时前往企二哥后台查看</param>
        /// <returns></returns>
        public async Task<RQTelCodeResponse> SendtemplateMessageAsync(string openID, string first, string keyword1, string keyword2,
            string keyword3, string keyword4, string keyword5, string remark
            )
        {
            WxAccessTokenResponse response = WxCommonApi.Instance().GetAccessTokenQeg(appid, secret);
            var relUri = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + response.access_token;
            string jsonStr = "{\"touser\":\"" + openID + "\",\"template_id\":\"40W_cp82phnNn4XdSC1Dtk1NdUp60x3lYytxe1ozluQ\",\"url\":\"\", \"miniprogram\":{\"appid\":\"\",\"pagepath\":\"\"},\"data\":{\"first\": {\"value\":\"" + first + "\",\"color\":\"#173177\"},\"keyword1\":{\"value\":\"" + keyword1 + "\",\"color\":\"#173177\"},\"keyword2\": {\"value\":\"" + keyword2 + "\",\"color\":\"#173177\"},\"keyword3\": {\"value\":\"" + keyword3 + "\",\"color\":\"#173177\"},\"keyword4\": {\"value\":\"" + keyword4 + "\",\"color\":\"#173177\"},\"keyword5\": {\"value\":\"" + keyword5 + "\",\"color\":\"#173177\" },\"remark\":{\"value\":\"" + remark + "\",\"color\":\"#173177\"}}}";
            return await RequestHandler.PostRequestAsync<RQTelCodeResponse>(new Uri(relUri), jsonStr);
        }



        /// <summary>
        /// 发送模板信息
        /// </summary>
        /// <param name="openID">推送人openid</param>
        /// <param name="first">[徐锐]您好，有新的客户分配给你</param>
        /// <param name="keyword1">客户来源 PC端</param>
        /// <param name="keyword2">客户姓名 徐锐</param>
        /// <param name="keyword3">客户电话 17751654676</param>
        /// <param name="keyword4">客户备注</param>
        /// <param name="keyword5">分配时间</param>
        /// <param name="remark">请及时前往企二哥后台查看</param>
        /// <returns></returns>
        public async Task<RQTelCodeResponse> SendtemplateMessageAsyncShare(string openID, string first, string keyword1, string keyword2,
            string keyword3, string keyword4, string keyword5, string remark
            )
        {
            WxAccessTokenResponse response = WxCommonApi.Instance().GetAccessTokenQeg(appid, secret);
            var relUri = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + response.access_token;
            string jsonStr = "{\"touser\":\"" + openID + "\",\"template_id\":\"RQSlW2jq89I1xviMvbX-LUqEn09stiOs-CfCs8mt8ZU\",\"url\":\"\", \"miniprogram\":{\"appid\":\"\",\"pagepath\":\"\"},\"data\":{\"first\": {\"value\":\"" + first + "\",\"color\":\"#173177\"},\"keyword1\":{\"value\":\"" + keyword1 + "\",\"color\":\"#173177\"},\"keyword2\": {\"value\":\"" + keyword2 + "\",\"color\":\"#173177\"},\"keyword3\": {\"value\":\"" + keyword3 + "\",\"color\":\"#173177\"},\"keyword4\": {\"value\":\"" + keyword4 + "\",\"color\":\"#173177\"},\"keyword5\": {\"value\":\"" + keyword5 + "\",\"color\":\"#173177\" },\"remark\":{\"value\":\"" + remark + "\",\"color\":\"#173177\"}}}";
            return await RequestHandler.PostRequestAsync<RQTelCodeResponse>(new Uri(relUri), jsonStr);
        }



        /// <summary>
        /// 根据openid获取用户信息
        /// </summary>
        /// <param name="openId"></param>
        public async Task<RQUserInfoResponse> UserInfo(string openId)
        {
            WxAccessTokenResponse response = WxCommonApi.Instance().GetAccessTokenQeg(appid, secret);
            var relUri = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN", response.access_token, openId);
            return await RequestHandler.RequestAsync<RQUserInfoResponse>(new Uri(relUri));
        }


        /// <summary>
        /// 新建新闻的ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<baiduUrlSendResponse> BaiDuUrlSend(int id)
        {
            var relUri = "http://data.zz.baidu.com/urls?site=www.qierge.com&token=gyMuxLjMIPIJZ8Ca";
            string jsonStr = string.Format("http://www.qierge.com/news/{0}.html", id);
            return await RequestHandler.PostRequestAsync<baiduUrlSendResponse>(new Uri(relUri), jsonStr);
        }
    }
}
